//
//  TestStockViewController.m
//  ZXKlineDemo
//
//  Created by wanmeizty on 2018/5/2.
//  Copyright © 2018年 郑旭. All rights reserved.
//

#import "TestStockViewController.h"
#import "ZXAssemblyView.h"
#import <Masonry.h>
#import "ZTYNetWorking.h"
#import "BGView.h"
#import "NextViewController.h"
/**
 * 总高度-->这里根据实际情况而定，为屏幕中给k线图预留位置的高度；根据实际需求
 k线图TotalHeight = 屏幕高度 - 其他自定义控件或者系统控件高度和
 */
#define TotalHeight (Portrait ? ((ZX_IS_IPHONE_X?((KSCREEN_HEIGHT-44-40-49-34)-(102+40+41+20+6)*HeightScale):(KSCREEN_HEIGHT-64-49)-(102+40+41+20+6)*HeightScale)) : KSCREEN_WIDTH)
/**
 * 总宽度
 */
#define TotalWidth      (Portrait ? (PortraitTotalWidth) : (LanscapeTotalWidth))
@interface TestStockViewController ()
/**
 *k线实例对象
 */
@property (nonatomic,strong) ZXAssemblyView *assenblyView;
/**
 *横竖屏方向
 */
@property (nonatomic,assign) UIInterfaceOrientation orientation;

@property (nonatomic,copy) NSString * type;
@property (nonatomic,strong) NSMutableArray * dataArray;
/**
 *当前绘制的指标名
 */
@property (nonatomic,strong) NSString *currentDrawQuotaName;
/**
 *
 */
@property (nonatomic,assign) TopChartContentType topChartContentType;
/**
 *所有的指标名数组
 */
@property (nonatomic,strong) NSArray *quotaNameArr;
@end

@implementation TestStockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100,100,10,2) cornerRadius:0];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor = RISECOLOR.CGColor;
    shapeLayer.strokeColor = RISECOLOR.CGColor;
    [self.view.layer addSublayer:shapeLayer];
    
    
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 100, 40)];
    [btn setTitle:@"点击" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
//    [self requestData];
//    [self.view addSubview:self.assenblyView];
    
    // Do any additional setup after loading the view.
}

- (void)click{
    NextViewController * nextVC = [[NextViewController alloc] init];
    [self.navigationController pushViewController:nextVC animated:NO];
}

- (void)requestData{
    
    self.type = @"1min";
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"type"] = self.type;
    param[@"market"] = @"btc_usdt";
    param[@"size"] = @"1000";
    
    [ZTYNetWorking postnetWorkingUrl:@"http://api.bitkk.com/data/v1/kline" params:param sucess:^(NSDictionary *response) {
        
        NSLog(@"response===>%@",response);
        [self performSelectorOnMainThread:@selector(getData:) withObject:response waitUntilDone:YES];
        
    } failure:^(NSError *error) {
        NSLog(@"error===%@",error);
    }];
}

- (void)getData:(NSDictionary *)dataDict{
    [self.view addSubview:self.assenblyView];
    [self addConstrains];
    
    //这句话必须要,否则拖动到两端会出现白屏
    self.automaticallyAdjustsScrollViewInsets = NO;
    //
    self.topChartContentType = TopChartContentTypeWithCandle;
    //
    self.currentDrawQuotaName = self.quotaNameArr[0];
    
    
    //监测旋转:用于适配横竖屏
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    
    NSArray *transformedDataArray =  [[ZXDataReformer sharedInstance] transformDataWithOriginalDataArray:[dataDict objectForKey:@"data"] currentRequestType:@"M1"];
    [self.dataArray addObjectsFromArray:transformedDataArray];
    //绘制k线图
    [self.assenblyView drawHistoryCandleWithDataArr:self.dataArray precision:5 stackName:@"股票名" needDrawQuota:self.currentDrawQuotaName];
    
    //如若有socket实时绘制的需求，需要实现下面的方法
    //socket
    //创建定时器,并设置代理接收
//    [ZXSocketDataReformer sharedInstance].delegate = self;
//    [[ZXSocketDataReformer sharedInstance] shouldToCreatTimerWithDataArray:self.dataArray];
    
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
}

- (void)addConstrains
{
    if (self.orientation == UIDeviceOrientationPortrait || self.orientation == UIDeviceOrientationPortraitUpsideDown) {
        //初始为竖屏
        self.navigationController.navigationBar.hidden = NO;
        [self.assenblyView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.view).offset(200);
            make.left.mas_equalTo(self.view);
            make.width.mas_equalTo(TotalWidth);
            make.height.mas_equalTo(TotalHeight);
            
        }];
        
    }else if (self.orientation==UIDeviceOrientationLandscapeLeft || self.orientation == UIDeviceOrientationLandscapeRight) {
        //初始为横屏
        self.navigationController.navigationBar.hidden = YES;
        [self.assenblyView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.view);
            make.left.mas_equalTo(self.view);
            make.width.mas_equalTo(TotalWidth);
            make.height.mas_equalTo(TotalHeight);
            
        }];
    }
    
}

#pragma mark - 屏幕旋转通知事件
- (void)statusBarOrientationChange:(NSNotification *)notification
{
    
    if (self.orientation == UIDeviceOrientationPortrait || self.orientation == UIDeviceOrientationPortraitUpsideDown) {
        
        //翻转为竖屏时
        [self updateConstrainsForPortrait];
        self.navigationController.navigationBar.hidden = NO;
    }else if (self.orientation==UIDeviceOrientationLandscapeLeft || self.orientation == UIDeviceOrientationLandscapeRight) {
        
        [self updateConstrsinsForLandscape];
        self.navigationController.navigationBar.hidden = YES;
    }
}
- (void)updateConstrainsForPortrait
{
    [self.assenblyView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view).offset(200);
        make.width.mas_equalTo(TotalWidth);
        make.height.mas_equalTo(TotalHeight);
    }];
    
}
- (void)updateConstrsinsForLandscape
{
    //翻转为横屏时
    [self.assenblyView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.width.mas_equalTo(TotalWidth);
        make.height.mas_equalTo(TotalHeight);
    }];
    
}

- (ZXAssemblyView *)assenblyView{
    if (!_assenblyView) {
        _assenblyView = [[ZXAssemblyView alloc] init];
//        _assenblyView.delegate = self;
    }
    return _assenblyView;
}

- (NSArray *)quotaNameArr
{
    if (!_quotaNameArr) {
        _quotaNameArr = @[@"MACD",@"KDJ",@"BOLL",@"RSI",@"VOL"];
    }
    return _quotaNameArr;
}

- (UIInterfaceOrientation)orientation
{
    return [[UIApplication sharedApplication] statusBarOrientation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
